import { _decorator, Component, Node, Sprite, SpriteFrame } from 'cc';
import { Block } from '../../src/Game/Block';
import { EventMgr } from '../../EventMgr/EventMgr';
const { ccclass, property } = _decorator;

@ccclass('BlockList002')
export class BlockList002 extends Component {

    @property(SpriteFrame)
    wallSp:SpriteFrame|null=null;
    @property(SpriteFrame)
    stoneSp:SpriteFrame|null=null;

    start() {
        EventMgr.Inst.addEvent("SAVE_CAMP",this.startEffect,this);
    }

    protected onDestroy(): void {
        EventMgr.Inst.removeEvent("SAVE_CAMP",this.startEffect,this);
    }

    itemEffect(n:Node,isStrong:boolean){
        let sp:Sprite =n.getComponent(Sprite);
        let bl:Block =n.getComponent(Block);
        if(isStrong){
            bl.bType=1;
            sp.spriteFrame=this.stoneSp;
        }else {
            bl.bType=0;
            sp.spriteFrame=this.wallSp;
        }
    }

    getBlockList():Node[]{
        let child:Node[] =this.node.children;

        let data=child.filter((item)=>{
            return item.name.includes('blockList');
        })

        let datas:Node[]=[];

        for(let item of data){
            for(let minItem of item.children){
                datas.push(minItem);
            }
        }
        return datas;
    }
    
    startEffect(){
        let datas =this.getBlockList();
        for(let item of datas){
            this.itemEffect(item,true);
        }
        this.scheduleOnce(()=>{
            let newDatas =this.getBlockList();
            //必须要重新拿因为可能有期间被销毁的
            for(let item of newDatas){
                this.itemEffect(item,false);
            }
        },15)
    }
}


